package com.ruoyi.web.controller.task;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.LevelCommentDto;
import com.ruoyi.common.core.domain.dto.TaskCommentReviewDto;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.task.domain.TaskComment;
import com.ruoyi.task.service.ITaskCommentService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 任务评论Controller
 *
 * @author KevenPotter
 * @date 2024-04-29 13:49:27
 */
@RestController
@RequestMapping("/task/comment")
@AllArgsConstructor
public class TaskCommentController extends BaseController {

    private final ITaskCommentService taskCommentService;
    private final TokenService tokenService;

    /**
     * 查询任务评论列表
     */
    @PreAuthorize("@ss.hasPermi('task:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskComment taskComment) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<TaskComment> list = taskCommentService.getList(taskComment);
        list.forEach(item -> taskCommentService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 根据[任务编号]查询任务评论列表
     *
     * @param taskId 任务编号
     * @author KevenPotter
     * @date 2024-04-30 14:09:25
     */
    @GetMapping("/listByTaskId/{taskId}")
    public AjaxResult listByTaskId(@PathVariable Long taskId) {
        List<LevelCommentDto> list = taskCommentService.getLevelComment(taskId);
        return AjaxResult.success(list);
    }

    /**
     * 导出任务评论列表
     */
    @PreAuthorize("@ss.hasPermi('task:comment:export')")
    @Log(title = "任务评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskComment taskComment) {
        List<TaskComment> list = taskCommentService.getList(taskComment);
        ExcelUtil<TaskComment> util = new ExcelUtil<TaskComment>(TaskComment.class);
        util.exportExcel(response, list, "任务评论数据");
    }

    /**
     * 获取任务评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        TaskComment taskComment = taskCommentService.getById(id);
        taskComment = taskCommentService.filterOutbound(taskComment, loginUser);
        return success(taskComment);
    }

    /**
     * 新增任务评论
     */
    @PreAuthorize("@ss.hasPermi('task:comment:add')")
    @Log(title = "任务评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskComment taskComment) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        taskComment = taskCommentService.filterInbound(taskComment, loginUser);
        return toAjax(taskCommentService.insertTaskComment(taskComment));
    }

    /**
     * 修改任务评论
     */
    @PreAuthorize("@ss.hasPermi('task:comment:edit')")
    @Log(title = "任务评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskComment taskComment) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        taskComment = taskCommentService.filterInbound(taskComment, loginUser);
        return toAjax(taskCommentService.updateTaskComment(taskComment));
    }

    /**
     * 删除任务评论
     */
    @PreAuthorize("@ss.hasPermi('task:comment:remove')")
    @Log(title = "任务评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(taskCommentService.deleteTaskCommentByIds(ids));
    }

    /**
     * 审核任务评论
     *
     * @param taskCommentReviewDto 任务评论审核数据传输
     * @return 返回是否审核成功
     * @author KevenPotter
     * @date 2024-04-29 15:08:15
     */
    @PreAuthorize("@ss.hasPermi('task:comment:review')")
    @Log(title = "任务评论", businessType = BusinessType.UPDATE)
    @PostMapping("/review")
    public AjaxResult review(@RequestBody TaskCommentReviewDto taskCommentReviewDto) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        taskCommentReviewDto.setReviewUserId(loginUser.getUserId());
        return toAjax(taskCommentService.review(taskCommentReviewDto));
    }
}
